﻿/*********************************************************************************/
/**    Pattern Preset Color Extractor                                           **/
/**    Copyright (C) 2012  Anja Knackstedt                                      **/
/**                                                                             **/
/**    This program is free software: you can redistribute it and/or modify     **/
/**    it under the terms of the GNU General Public License as published by     **/
/**    the Free Software Foundation, either version 3 of the License, or        **/
/**    (at your option) any later version.                                      **/
/**                                                                             **/
/**    This program is distributed in the hope that it will be useful,          **/
/**    but WITHOUT ANY WARRANTY; without even the implied warranty of           **/
/**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            **/
/**    GNU General Public License for more details.                             **/
/**                                                                             **/
/**    You should have received a copy of the GNU General Public License        **/
/**    along with this program.  If not, see <http://www.gnu.org/licenses/>.    **/
/**                                                                             **/
/*********************************************************************************/

using System.IO;

namespace PatternPresetColorExtractor
{
    class Sims3PackXmlReader
    {
        /// <summary>
        /// reads xml part and start position of the binary part from a Sims3Pack file
        /// </summary>
        /// <param name="path">path of the sims3pack file</param>
        /// <returns></returns>
        public PatternPackage read(string path)
        {
            PatternPackage patternPackage = new PatternPackage(path);           

            // Sims3Pack:
            // DWORD length of the following string
            // String
            // WORD
            // DWORD length of the following xml part

            int DWORD = 4;
            int WORD = 2;

            string XMLPart = "";

            BinaryReader binaryReader = new BinaryReader(new FileStream(path, FileMode.Open, FileAccess.Read));

            //read 1. DWORD 
            byte[] DWordLength = binaryReader.ReadBytes(DWORD);
            int stringLength = getLengthFromDWord(DWordLength);

            //skip string part and WORD         
            binaryReader.BaseStream.Position += stringLength + WORD;

            //read 2. DWORD 
            DWordLength = binaryReader.ReadBytes(DWORD);
            int xmlLength = getLengthFromDWord(DWordLength);

            // read XML part and convert it
            byte[] XMLBytes = binaryReader.ReadBytes(xmlLength);
            XMLPart = System.Text.UTF8Encoding.UTF8.GetString(XMLBytes);            

            binaryReader.Close();

            patternPackage.BinaryStartPosition = DWORD + stringLength + WORD + DWORD + xmlLength;
            patternPackage.Xml = XMLPart;

            return patternPackage;
        }

        /// <summary>
        /// gets length from a DWORD
        /// e.g. 4C 0A 00 00 -> A4C
        /// </summary>
        /// <param name="dword">DWord as Byte Array (4 Bytes)</param>
        /// <returns>length</returns>
        private static int getLengthFromDWord(byte[] dword)
        {
            int lenght = 0;
            
            {
                int byte0 = dword[0];
                int byte1 = dword[1] * 0x100;
                int byte2 = dword[2] * 0x10000;
                int byte3 = dword[3] * 0x1000000;

                lenght = byte0 + byte1 + byte2 + byte3;
            }


            return lenght;
        }
    }
}
